﻿using System.Collections.Generic;
using System.Data;
using MaLu.Data;
using System;

namespace MaLu.BusiLogic
{
   public class BLProject
    {
       public static DataTable SaveProject(IDbProvider provider, Guid pid,string name ,Guid deptid,string deptname,string deptdspname)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@ProjectID", DataType.GUID, pid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Name", DataType.VarChar, 200, name));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentID", DataType.GUID, deptid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentName", DataType.VarChar, 50, deptname));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentDisplayName", DataType.VarChar, 50, deptdspname));
         
           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PSaveProject", paraList.ToArray());
           return ds.Tables[0];
       }
       public static int DeleteProject(IDbProvider provider, Guid pid)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@ProjectID", DataType.GUID, pid));

           int result = 0;
           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PDeleteProject", paraList.ToArray());
           int.TryParse(ds.Tables[0].Rows[0][0].ToString(), out result);
           return result;
       }

       public static string GetTemplate(IDbProvider provider,string pagename)
       {
           string template = string.Empty;
           DataTable ds = SqlHelper.Instance.ExecuteDataTable(provider, "select DataTemplate from PFormItem where PageName='" + pagename + "'");
           if (ds.Rows.Count > 0)
           {
               if (ds.Rows[0][0] != null)
                   template = ds.Rows[0][0].ToString();
           }
           return template;
       }
       public static string GetProjectItem(IDbProvider provider, Guid pid, string pagename ,ref string projectname)
       {
           string template = string.Empty;
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@PageName", DataType.VarChar, 200, pagename));
           paraList.Add(SqlHelper.Instance.CreateParameter("@ProjectID", DataType.GUID, pid));

           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PGetProjectContent", paraList.ToArray());
           if (ds.Tables[0].Rows.Count > 0)
           {
               template = ds.Tables[0].Rows[0][0].ToString();
           }
           if (ds.Tables[1].Rows.Count > 0)
           {
               projectname = ds.Tables[1].Rows[0][0].ToString();
           }


           return template;
       }
       public static void SaveProjectItem(IDbProvider provider, Guid pid, string pagename, string content)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();          
           paraList.Add(SqlHelper.Instance.CreateParameter("@PageName", DataType.VarChar, 200, pagename));
           paraList.Add(SqlHelper.Instance.CreateParameter("@ProjectID", DataType.GUID, pid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Content", DataType.Text, content));

           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PSaveItem", paraList.ToArray());
         
       }

       public static DataTable GetProjectDetailList(IDbProvider provider, int Year)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();
          
           paraList.Add(SqlHelper.Instance.CreateParameter("@Year", DataType.Int32, 8, Year));
          
           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_GetProjectDetailList", paraList.ToArray());

           return ds.Tables[0];
         
       
       }
    } 
}
